function ViewControl(){
	this.formElements = new Array;
	this.eventElements = new Array;
	this.addEventElement = function(elementId, eventName){
		this.eventElements[this.eventElements.length] = new FormElemEvent(elementId, eventName);		
	}
	this.addElement = function(name, value){
		this.addEventElement(name,'change');
		this.formElements[this.formElements.length] = new FormElem(name, value);
	}
	
	this.addTextElement = function(name, value){
		this.addEventElement(name,'change');
		this.formElements[this.formElements.length] = new TextElem(name, value);
	}
	
	this.refreshView = function(){
		
		for(i=0; i<this.formElements.length; i++){
			this.formElements[i].refreshElem();		
		}
	}
	
	
	///////////////////////////
	this.windowOnLoadEvent = function(){		
		myEvent = document.createEvent("HTMLEvents"); 
		if( document.createEventObject ) {
			myEvent = document.createEventObject(); 
		}
		
		myEvent.initEvent("load", true, false);
		
		return myEvent;
	}	
	
}


/////////////////////////////////////////////////////////////////////

function FormElemEvent(_elementId, _eventName){
	this.elemId = _elementId;
	this.eventName = _eventName;
}

//////////////////////////////////////////////////////////////////////

function addViewEventListener(elem, eventType, expression){
		element = document.getElementById(elem);
		if(element == null){
			return false;
		}
		if(window.addEventListener) { // Standard
			element.addEventListener(eventType, expression, true);
			return true;
		}
		if(window.attachEvent) { // IE
			element.attachEvent('on' + eventType, expression);
			return true;
		}
		return false;
}	


function precessEvent(event){	
	element = event.target;
	if(window.attachEvent) { // IE
		element = event.srcElement;
	}	
	elemId = element.id;
	alert(elemId + ' is changed');	
} 

function onLoadWindow(event){
	onLoadView(viewControl);	
}

function onLoadView(viewControl){
	eventElems = viewControl.eventElements;	
	for(i=0; i<eventElems.length; i++){
		addViewEventListener(eventElems[i].elemId,eventElems[i].eventName,precessEvent);		
	}
	
	viewControl.refreshView();
}



/////////////////////////////////Form Element //////////////////////////////////////////////

function FormElem(id, value){
	this.elemId = id;
	this.elemValue = value;	
	this.events = new Array(); //
	this.updateValue = function(element, value){		
		element.value = value;
	}
	this.refreshElem = function(){
		element = document.getElementById(this.elemId);
		if(element == null){
			return false;
		}
		this.updateValue(element, this.elemValue);		
	}	
}

function TextElem(id, value){	
	this.inheritFrom = FormElem;
	this.inheritFrom(id, value);	
		  	
  	this.updateValue = function(element, value){  		
		element.innerHTML = value;
	}
}


//////////////////////////// AJAX ////////////////////////////////////////////////////


function getHTTPObject(){
	if (window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (window.XMLHttpRequest){
		return new XMLHttpRequest();
	}
	alert("Your browser does not support AJAX.");
	return null;
}







